<?php
require Config::ROOT . '/modules/ComGate/xmlrpc.inc';
require Config::ROOT . '/modules/ComGate/xmlrpc_wrappers.inc';
require Config::ROOT . '/modules/ComGate/xmlrpcs.inc';
require Config::ROOT . '/modules/ComGate/client.php';

class ComGate extends Module {
    protected $name = 'ComGate';
    protected $version = '2009-12-23';
    protected $administrable = true;
    protected $details = array('dependencies' => array(),
            'observes' => array('ComGate'));

    const LOG = 'comGateLog';

    protected $logTable = array('id' => 'iPN+', 'user' => 'iN', 'time' => 'iN',
            'amount' => 'iN');

    protected $logRecord = array('table' => self::LOG, 'data' => array());

    public function  __construct(DBAPI $dbapi, RecordList $recordList) {
        parent::__construct($dbapi, $recordList);
        $this->logRecord['properties'] = $this->logTable;
    }

    public function install() {
        Modules::getModule('Users')->registerRights($this->getName(),
                $this->getRights());
        return $this->dbdriver->createTable(self::LOG, $this->logTable);
    }

    public function uninstall() {
        return $this->dbdriver->dropTable(self::LOG);
    }

    public function getUserHistory($uid) {
        $cols = array('time', 'amount');
        $history = &$this->dbdriver->select(self::LOG, $cols, '`user` = ' .
                (int) $uid);
        return $history;
    }

    public function display($param = null) {
        if (Kernel::getMode() == Kernel::ADMIN) {
            return parent::display($param);
        }
        if (isset($_POST['methName'])) {
            if (rawurldecode($_POST['methName']) == 'NewDLR') {
                $this->newDelivery();
            } elseif ((rawurldecode($_POST['methName']) == 'NewSMS') ||
                    ((rawurldecode($_POST['methName']) == ''))) {
                $this->newSMS();
            } else{
                header("HTTP/1.0 400 Bad Request");
                header('Content-Type: text/plain');
                printf("ERROR unknown method\r\n");
                flush();
            }
        }
    }

    public function displayAdminIndex() {
        $cols = array('user', 'time', 'amount');
        $order = array('!id');
        $log = &$this->dbdriver->select(self::LOG, $cols, '', $order);
        foreach ($log as &$item) {
            $user = Modules::getModule('Users')->getUser($item['user']);
            $item['user'] = $user['data']['login'];
        }
        $params = array('log' => &$log);
        $this->template('log', $params);
    }

    private function newDelivery() {
        header('Content-Type: text/plain');
        printf("OK\r\n");
        flush();
    }

    private function newSMS() {
        $names = array('id', 'appnumber', 'phone', 'data', 'scts', 'price',
                'operator', 'service', 'refNo', 'maxNo');
        $params = array();
	for($i = 0; $i < count($names); $i++) {
            $params[$names[$i]] = rawurldecode($_POST[$names[$i]]).'';
	}
        $uid = (int) substr($params['data'], 4);
        $user = Modules::getModule('Users')->getUser($uid);
        $visitor = Modules::getModule('Visitors')->getVisitor($uid);
        if (isset($visitor['data'])) {
            $visitor['data']['account'] += 9;
            $provision = $visitor['data']['provisionID'];
            $visitor['data']['provisionID'] = 0;
            $this->dbapi->update($visitor);
            if ($provision > 0) {
                $visitor = Modules::getModule('Visitors')->getVisitor($provision);
                $visitor['data']['account'] += 10;
                $this->dbapi->update($visitor);
            }
            $msg = 'Vazeny uzivateli ' . $user['data']['login'] .
                    ', platba byla prijata. Clicker.cz';
        } else {
            $msg = 'Uzivatel nebyl nalezen. Zkontrolujte prosim zadane ' .
                    'udaje nebo se obratte na technickou podporu. Clicker.cz';
        }
        $log = $this->logRecord;
        $log['data'] = array('user' => $uid, 'time' => time(), 'amount' => 9);
        $this->dbapi->save($log);
        header('Content-Type: text/plain');
	printf("OK\r\n");
        printf(
                "phone=%s&data=%s&type=%d&value=%d&partnerId=%s\r\n",
                rawurlencode($params['phone']),
                rawurlencode($msg),
                0,
                0,
                rawurlencode('31134')
	);
	flush();
    }
}
?>
